﻿--[[
   @brief    web_register for all lua event register by js call.
   @author   harleyxwang
   @version  1.0
   @date     2016-03-05
--]]

web_register_center =
{
    register_events = {},   
    -- {game_id={[1] = {name = event_name, browser = browser_id, callback = event_callback, src=src_id},
                 -- [2] = {name = event_name, browser = browser_id, callback = event_callback, src=src_id}}

    init = function()
        base_utility.log("web_register_center.init called");
        local tpf_ui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        tpf_ui:RegisterJsEvent("register_event", "web_register_center.handle_register_event");
        tpf_ui:RegisterJsEvent("unregister_event", "web_register_center.handle_unregister_event");
        tpf_ui:RegisterJsEvent("unregister_all_event", "web_register_center.handle_unregister_all_event");
    end;

    ---废弃，统一用RegisterJsEvent注册感兴趣的js事件
    on_js_callback = function(uilib, func_name, json, browser_id)
        --[[
        if func_name == "register_event" then
            web_register_center.handle_register_event("", json, browser_id);
        elseif func_name == "unregister_event" then
            web_register_center.handle_unregister_event("", json, browser_id);
        elseif "unregister_all_event" == func_name then
            web_register_center.handle_unregister_all_event("", "", browser_id);
        end
        --]]
    end;

    handle_register_event = function(func_name, json, browser_id)
        local param_table = base_utility.json_to_table(json);
        if param_table == nil then return end
        local game_id = "";         -- defualt value is empty
        local event_name = nil;
        local event_callback = nil;
        local src_id = 0;
        if param_table.data ~= nil then
            if param_table.data.game_id ~= nil then
                 game_id = tostring(param_table.data.game_id);
            end
            event_name = param_table.data.event_name;
            event_callback = param_table.data.event_callback;
            if param_table.src ~= nil and param_table.src ~= "" then
                src_id = tonumber(param_table.src);
            end
        end
        if event_name ~= nil and event_callback ~= nil then
            if web_register_center.register_events[game_id] == nil then
                web_register_center.register_events[game_id] = {}
            end
           
            local  find_event = false;
            for idx_, info_table in ipairs(web_register_center.register_events[game_id]) do
                if event_name == info_table.name and  browser_id == info_table.browser then
                    find_event = true;
                    break;
                end
            end
            -- 如果不存在则注册
            if find_event ~= true then
                table.insert(web_register_center.register_events[game_id], 
                    {name = event_name, browser = browser_id, callback = event_callback, src = src_id});
                -- 抛出注册成功的事件通知
                local register_param = {event_name = event_name, game_id = game_id, data = param_table.data};
                event_center.send_event(web_register_event.REGISTER_SUCCESS_EVENT, 
                    base_utility.table_to_json(register_param));
            end 
            --回复注册成功的包
            web_register_center.response_for_register_event(json, 0, browser_id);
        else 
            -- 回复注册失败的包 
            web_register_center.response_for_register_event(json, 1, browser_id);
        end;
    end;

    handle_unregister_event = function(func_name, json, browser_id)
        local param_table = base_utility.json_to_table(json);
        if param_table == nil then return end
        local game_id = "";            -- defualt value is empty
        if param_table.src ~= nil and param_table.src ~= "" then
            game_id = tostring(param_table.src);
        end
        local event_name = nil;
        if param_table.data ~= nil then
            if param_table.data.game_id ~= nil then
                 game_id = tostring(param_table.data.game_id);
            end
            event_name = param_table.data.event_name;
        end
        if event_name ~= nil and web_register_center.register_events[game_id] ~= nil then
            for idx_, info_table in ipairs(web_register_center.register_events[game_id]) do
                if event_name == info_table.name and  browser_id == info_table.browser then
                    table.remove(web_register_center.register_events[game_id], idx_);
                    break;
                end
            end
        end
        -- 回复反注册成功的包
        web_register_center.response_for_register_event(json, 0, browser_id);
    end;
    -- table从后往前删除元素
    handle_unregister_all_event = function(func_name, json, browser_id)
        for game_idx_, game_table_ in pairs(web_register_center.register_events) do
            if game_table_ ~= nil and type(game_table_) == 'table' then
                for idx_ = #game_table_, 1, -1 do
                    if browser_id == game_table_[idx_].browser then
                        table.remove(game_table_, idx_);
                    end
                end
            end
        end
    end;
    response_for_register_event = function(json, ret_code, browser_id)
        local param_table = base_utility.json_to_table(json);
        if param_table == nil then return end
        
        local rsp_table = {};
        rsp_table.retCode = ret_code;
        rsp_table.msg = "";
        rsp_table.seq_num = param_table.seq_num;
        rsp_table.src = param_table.src;
        rsp_table.dest = param_table.dest;

        if param_table.callback_func ~= nil and param_table.callback_func ~= "" then
             web_utility.lua_call_js_by_id_ex(param_table.callback_func, 
                base_utility.table_to_json(rsp_table), browser_id);
        end
    end;
    -- 如果找到了事件对应的回调函数，则返回回调函数和src_id，否则返回nil
    -- 如果game_id填0或者nil，则表示注册的事件跟游戏类型无关
    get_func_by_browser = function(event_name, custom_browser_id, game_id_param)
        local  game_id = "";
        if game_id_param ~= nil then
            game_id = tostring(game_id_param);
        end
        if event_name == nil then return end
        if web_register_center.register_events[game_id] == nil then
            return;
        end
        local browser_id = ui_utility.uilib():GetControlIDbyCustomID(custom_browser_id);
        local callback_name = nil;
        local src_id = 0;
        for idx_, info_table in ipairs(web_register_center.register_events[game_id]) do
            if event_name == info_table.name and browser_id == info_table.browser then
                callback_name = info_table.callback;
                src_id = info_table.src;
                break;
            end
        end
        return callback_name, src_id;
    end;
};

web_register_center.init();
